function fun_a() {
    var jumpNumTxt = $("#jumpNumTxt").val();
    var pageCount = $("#pageCount").val();
    var pager = $("#pager").val();

    if (jumpNumTxt == null || jumpNumTxt == "") {
        alert("不能为空！");
    } else if (jumpNumTxt < 1) {
        alert("跳转页面不能小于1！");
    } else if (parseInt(jumpNumTxt) > parseInt(pageCount)) {
        alert("跳转页不能大于总页数！");
    } else if (parseInt(jumpNumTxt) == parseInt(pager)) {
        alert("不能跳转当前页！");
    } else {
        location.href = "/user/userAdmin?page=" + jumpNumTxt;
    }
}

$(function () {
    //给全选的复选框添加事件
    $("#all").click(function () {
        // this 全选的复选框
        var userids = this.checked;
        //获取name=box的复选框 遍历输出复选框
        $("input[name=box]").each(function () {
            this.checked = userids;
        });
    });

    //给name=box的复选框绑定单击事件
    $("input[name=box]").click(function () {
        //获取选中复选框长度
        var length = $("input[name=box]:checked").length;
        //未选中的长度
        var len = $("input[name=box]").length;
        if (length == len) {
            $("#all").get(0).checked = true;
        } else {
            $("#all").get(0).checked = false;
        }
    });

});

function addUser() {

    var flage = true;
    var username = $("#username").val();
    var name = $("#name").val();
    var department = $("#department").val();
    var userRole = $("#userRole").val();
    var post = $("#post").val();
    var userAptitude = $("#userAptitude").val();
    var password = $("#password").val();


    function showErrorMess(ms) {
        flage = false;
        $("#hint").html(ms);
    }

    if (username == "" || username == null) {
        showErrorMess("用户ID不能为空！");
        return false;
    }

    var abc = '';
    $.ajax({
        type: "GET",
        url: "/user/AjaxVerifyUserName",
        dataType: 'json',
        data: {"username": username},
        async: false,
        success: function (data) {
            if (data) {
                abc = 'OK';
            } else {
                abc = 'NO';
            }
        }
    });

    if (abc == 'NO') {
        showErrorMess("该用户ID已存在！");
        return false;
    }

    if (name == "" || name == null) {
        showErrorMess("姓名不能为空！");
        return false;
    }

    if (department == 0) {
        showErrorMess("请选择部门！");
        return false;
    }

    if (userRole == 0) {
        showErrorMess("请选择角色！");
        return false;
    }

    if (post < 0) {
        showErrorMess("请选择岗位！");
        return false;
    }

    if (userAptitude == "" || userAptitude == null) {
        showErrorMess("请输入人员资质！");
        return false;
    }

    if (password == "" || password == null) {
        showErrorMess("请输入密码！");
        return false;
    }

    return true;
}

function js_unsteady() {
    var obj = document.getElementsByName("box");
    alert(obj.toString());
    var flag = false;

    for (var i = 0; i < obj.length; i++) {
        if (obj[i].checked) {
            flag = true;
            break;
        }
    }

    if (!flag) {
        alert("请选择用户！");
    }


    var listOK = [];
    var listNO = [];
    for (var k in obj) {
        if (obj[k].checked) {
            $("#123").val();
            $("#1").val()
            if ($("#" + obj[k].value).val() == "1") {
                alert(obj[k].value + "ok");
                listOK.push(obj[k].value);
            } else if ($("#" + obj[k].value).val() == "0") {
                alert(obj[k].value + "on");
                listNO.push(obj[k].value);
            }
        }
    }

    var status;
    var check_val = [];
    if (listOK.length > listNO.length) {
        status = 0;
        check_val = listOK;
    } else {
        status = 1;
        check_val = listNO;
    }

    if (check_val.length > 0) {
        if (confirm("您确定要继续吗？")) {
            $.ajax({
                type: "GET",
                traditional: true,//阻止深度序列化
                url: "/user/AjaxDelete",
                dataType: 'json',
                data: {"ids": check_val, "status": status},
                async: false,
                contentType: "application/json; charset=utf-8",
                success: function (feedback) {
                    if (feedback == true) {
                        location.reload(true);
                    }
                }
            });
        }
    }
}

function js_delete() {
    var obj = document.getElementsByName("box");

    var flag = false;

    for (var i = 0; i < obj.length; i++) {
        if (obj[i].checked) {
            flag = true;
            break;
        }
    }

    if (!flag) {
        alert("请选择用户！");
    }

    var check_val = [];
    for (var k in obj) {
        if (obj[k].checked) {
            check_val.push(obj[k].value);
        }
    }
    if (check_val.length > 0) {
        if (confirm("您确定要删除吗？")) {
            $.ajax({
                type: "GET",
                traditional: true,//阻止深度序列化
                url: "/user/AjaxDelete",
                dataType: 'json',
                data: {"ids": check_val, "status": 0},
                async: false,
                contentType: "application/json; charset=utf-8",
                success: function (feedback) {
                    if (feedback == true) {
                        location.reload(true);
                    }
                }
            });
        }
    }


}

function resetPwd() {
    var obj = document.getElementsByName("box");

    var flag = false;

    for (var i = 0; i < obj.length; i++) {
        if (obj[i].checked) {
            flag = true;
            break;
        }
    }

    if (!flag) {
        alert("请选择要重置密码的用户！");
    }

    var check_val = [];
    for (var k in obj) {
        if (obj[k].checked) {
            check_val.push(obj[k].value);
        }
    }
    if (check_val.length > 0) {
        if (confirm("您确定要重置密码吗？")) {
            $.ajax({
                type: "GET",
                traditional: true,//阻止深度序列化
                url: "/user/AjaxResetPwd",
                dataType: 'json',
                data: {"ids": check_val},
                async: false,
                contentType: "application/json; charset=utf-8",
                success: function (feedback) {
                    if (feedback == true) {
                        location.reload(true);
                    }
                }
            });
        }

    }


}

function js_method() {
    var obj = document.getElementsByName("box");

    var flag = false;

    for (var i = 0; i < obj.length; i++) {
        if (obj[i].checked) {
            flag = true;
            break;
        }
    }

    $("#updateA").attr('data-toggle', 'modal');
    $("#updateA").attr('data-target', '#update');

    if (!flag) {
        alert("请选择你要修改的用户！");
        $("#updateA").removeAttr("data-toggle");
        $("#updateA").removeAttr("data-target");
    }

    var check_val = [];
    for (var k in obj) {
        if (obj[k].checked) {
            check_val.push(obj[k].value);
        }
    }

    if (check_val.length > 1) {
        alert("暂时只支持单个修改！");
        $("#updateA").removeAttr("data-toggle");
        $("#updateA").removeAttr("data-target");
    }

    if (check_val.length == 1) {
        $.ajax({
            type: "GET",
            url: "/user/AjaxUserById",
            dataType: 'json',
            data: {"id": check_val.toString()},
            async: false,
            success: function (data) {
                $("#UserId").val(data.byId.id);
                $("#username2").val(data.byId.username);
                $("#pwd").val(data.byId.password);
                $("#name2").val(data.byId.name);
                document.getElementById("department2").value = data.byId.department;
                document.getElementById("userRole2").value = data.byId.userRole;
                document.getElementById("post2").value = data.byId.post;
                $("#userAptitude2").val(data.byId.userAptitude);
            }
        });

    }

}

function updateUser() {
    var id = $("#UserId").val();
    var username = $("#username2").val();
    var name = $("#name2").val();
    var department = $("#department2").val();
    var userRole = $("#userRole2").val();
    var post = $("#post2").val();
    var userAptitude = $("#userAptitude2").val();

    var flage = true;

    function showErrorMess(ms) {
        flage = false;
        $("#updateHint").html(ms);
    }

    if (id == "" || id == null) {
        showErrorMess("你真牛逼，都搞出空指针异常了。");
        return false;
    }
    if (username == "" || username == null) {
        showErrorMess("用户ID不能为空！");
        return false;
    }
    if (name == "" || name == null) {
        showErrorMess("姓名不能为空！");
        return false;
    }
    if (department == 0) {
        showErrorMess("请选择部门！");
        return false;
    }
    if (userRole == 0) {
        showErrorMess("请选择角色！");
        return false;
    }
    if (post < 0) {
        showErrorMess("请选择岗位！");
        return false;
    }
    if (userAptitude == "" || userAptitude == null) {
        showErrorMess("请输入人员资质！");
        return false;
    }
    return true;
}